﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using MVC4WPF;

namespace $rootnamespace$
{
    /// <summary>
    /// Controller class for the view '$ViewContractName$'
    /// </summary>
    public partial class $safeitemname$ : $BaseType$<$ViewContractName$>$ControllerContractName$$ControllerModelContractName$
    {
        #region Constructor

        /// <summary>
        /// Initial constructor for the the '$safeitemname$', the view is subscribed to during construction.
        /// </summary>
        /// <param name="viewContract">The instance of the view passed in as the view contract interface.</param>
        public $safeitemname$($ViewContractName$ viewContract)
            : base(viewContract)
        {
            try
            {
                //Initilizing the controller
                InitializeController();
            }
            catch
            {
                //swallowing exceptions on construction
            }
        }

        /// <summary>
        /// Initial constructor for the the class '$safeitemname$'. 
        /// The view is subscribed to during construction, and the parent window for this view is set.
        /// </summary>
        /// <param name="viewContract">The instance of the view passed in as the view contract interface.</param>
        /// <param name="parentWindow">The parent window for this </param>
        public $safeitemname$($ViewContractName$ viewContract, Window parentWindow)
            : base(viewContract)
        {
            try
            {
                //Setting the parent window
                this.ParentWindow = parentWindow;

                //Initilizing the controller
                InitializeController();
            }
            catch
            {
                //swallowing exceptions on construction
            }
        }$if$ ($ControllerUsesModelContract$ == True)

        /// <summary>
        /// Initial constructor for the class '$safeitemname$'
        /// The view is subscribed to during construction, and the model contract is set.
        /// </summary>
        /// <param name="viewContract">The instance of the view passed in as the view contract interface.</param>
        /// <param name="modelContract">The instance of the model passed in as the model contract interface.</param>
        public $safeitemname$($ViewContractName$ viewContract, $ModelContractName$ modelContract)
            : base(viewContract)
        {
            try
            {
                //Setting the contract from the model on the controller
                this.ModelContract = modelContract;

                //Initilizing the controller
                InitializeController();
            }
            catch
            {
                //swallowing exceptions on construction
            }
        }

        /// <summary>
        /// Initial constructor for the the class '$safeitemname$'. 
        /// The view is subscribed to during construction, and the parent window for this view is set.
        /// </summary>
        /// <param name="viewContract">The instance of the view passed in as the view contract interface.</param>
        /// <param name="parentWindow">The parent window for this </param>
        /// <param name="modelContract">The instance of the model passed in as the model contract interface.</param>
        public $safeitemname$($ViewContractName$ viewContract, Window parentWindow, $ModelContractName$ modelContract)
            : base(viewContract)
        {
            try
            {
                //Setting the parent window
                this.ParentWindow = parentWindow;

                //Setting the contract from the model on the controller
                this.ModelContract = modelContract;

                //Initilizing the controller
                InitializeController();
            }
            catch
            {
                //swallowing exceptions on construction
            }
        }$endif$

        /// <summary>
        /// Helper method that is called by both constructor methods that initializes the controller class,
        /// and the controller contract.
        /// </summary>
        private void InitializeController()
        {
            //Subscribing to the events that are implemented on the view contract.
            this.SubscribeToViewContractEvents();$if$($ControllerUsesModelContract$ == True)
            
            //Subscribing to the events that are implemented on the model contract
            this.SubscribeToModelContractEvents();$endif$

            //Configuring the controller contract implemented by this controller.
            this.ConfigureController();
        }

        /// <summary>
        /// Helper method that subscribes to events from the view contract. 
        /// The IWPFView events have been subscribed to in the controller base class.
        /// </summary>
        private void SubscribeToViewContractEvents()
        {           
            $SubscribeToEventTemplateContent$  
        }$if$($ControllerUsesModelContract$ == True)

        private void SubscribeToModelContractEvents()
        {
            $SubscribeToModelEventTemplateContent$
        }$endif$

        #endregion

        #region ViewContract - Event Handlers
        $EventHandlerTemplateContent$
        #endregion$if$($ControllerUsesModelContract$ == True)

        #region ModelContract - Event Handlers
        $ModelEventHandlerTemplateContent$
        #endregion$endif$
        
        #region Release View Event Handlers
        
        /// <summary>
        /// Releases event handler methods from the view contract events they were handling.
        /// </summary>
        private void ReleaseViewEventHandlers()
        {
            if(ViewContract != null)
            {
                $ReleaseEventHandlerTemplateContent$
            }
        }

        #endregion$if$($ControllerUsesModelContract$ == True)

        #region Release Model Event Handlers
        
        /// <summary>
        /// Releases event handler methods from the model contract events they were handling.
        /// </summary>
        private void ReleaseModelEventHandlers()
        {
            if (ModelContract != null)
            {
                $ReleaseModelEventHandlerTemplateContent$
            }
        }

        #endregion$endif$
        
        #region Override Implementation of IDisposable

        /// <summary>
        /// Disposes all managed and unmanaged resources from this class
        /// </summary>
        /// <param name="disposing">Boolean flag the determines if the managed resources are to be disposed</param>
        /// <remarks>This is not thread safe. Dispose should only be called from an owning thread.</remarks>
        protected override void Dispose(bool disposing)
        {
            //Confirming that the class has not already been disposed
            if (!this._disposed)
            {
                try
                {
                    //Determining if managed resources should be release
                    if (disposing)
                    {
                        //Releasing all managed resources
                        DisposeManagedResourcesFor$safeitemname$();
                    }

                    //Releasing all unmanaged resources
                    DisposeUnmanagedResourcesFor$safeitemname$();

                }
                catch
                {
                    //Swallow dispose exceptions.
                }
                finally
                {
                    //Calling the base classes dispose
                    base.Dispose(disposing);
                }
            }
        }

        #endregion

        #region IDisposable Helper Methods

        /// <summary>
        /// Releases all managed resources from this class.
        /// </summary>
        private void DisposeManagedResourcesFor$safeitemname$()
        {
            try
            {
                //Releases the handlers to events subscribed to on the view contract.
                ReleaseViewEventHandlers();$if$($ControllerUsesModelContract$ == True)

                //Releases the handlers to events subscribed to on the model contract.
                ReleaseModelEventHandlers();$endif$
                
                //Releases resources that were created for the controller contract.
                ReleaseContractResources();
                
                //TODO: DisposeManagedResourcesFor$safeitemname$ - Add dispose logic for all managed resources within this class.
            }
            catch
            {
                //Swallowing exceptions during dispose.
            }
        }

        /// <summary>
        /// Releases all unmanaged instance based resources.
        /// </summary>
        private void DisposeUnmanagedResourcesFor$safeitemname$()
        {
            try
            {
                //TODO: DisposeUnmanagedResourcesFor$safeitemname$ - Add dispose logic for all unmanaged resources within this class.
            }
            catch
            {
                //Swallowing exceptions during dispose.
            }
        }
        #endregion
    }
}
